<?php
require './include/global.php';
require './include/validate.php';
require './include/all_left.php';
include("FCKeditor/fckeditor.php") ;

//Not A Recuiter
$id = $_GET['id'] + 0;
if($id < 1 || $_SESSION[usertype] != "recruiter"){
   echo "<meta http-equiv=\"refresh\" content=\"0;URL=errorall.php?errid=rc_1012\">";
   exit;	
}
$sql = "SELECT * FROM pa_email WHERE e_id = '".$id."'";
$rs = $DB->fetch_one_array($sql);
$smarty->assign('s',$rs);


//Application job related
if($rs['e_joid']>0){
	$sql = "SELECT jo_title FROM pa_job WHERE jo_id = '".$rs['e_joid']."'";
	$_jobrelated = $DB->fetch_one_array($sql);
	$smarty->assign("jobtitle",$_jobrelated['jo_title']);
	$smarty->assign("jobid",$rs['e_joid']);
}

if($rs['e_receivedrcid'] != $_SESSION['uid']){
   echo "<meta http-equiv=\"refresh\" content=\"0;URL=errorall.php?errid=rc_1012\">";
   exit;	
}

$smarty->display('header.tpl');
$smarty->assign("date",date("m-d-Y"));
$smarty->assign("from",$_SESSION['realname']."[".$_SESSION['useremail']."]");


if($_GET['action']=="send" ){
	  
	  $err = '';
	  if($_GET['action'] == "send"){//send
			if(trim($_POST['subject'])==''){
			  $err .= "Subject is required<br>";
			}
			if (trim(strip_tags($_POST['content'])) == '')
			{
			$err .= "Message is required<br>";
			}  
	  }
	  $to_emails = array($rs['e_fromemail']);
	  $to_names = array($rs['e_fromname']);
	  //update the original one
		$sql = "UPDATE pa_email SET `e_isreplied` = 1 WHERE e_id = '".$rs['e_id']."' AND e_receivedrcid = '".$_SESSION['uid']."'";
		$DB->query($sql);
	 
	  //end
	  if($err == ''){
		    if($_GET['action'] == "send"){//send
				//Save Email
				$sql = " INSERT INTO pa_email (					
					`e_sentrcid`,
					`e_receivedrcid`,
					`e_sendertype`,
					`e_receivertype`,
					`e_type`,
					`e_fromemail`,
					`e_fromname`,
					`e_toemail`,
					`e_toname`,
					`e_joid`,
					`e_subject`,
					`e_content`,
					`e_contactid`,
					`e_created`							
				) VALUES (
					 '".$_SESSION['uid']."',
					 '".$rs['e_sentrcid']."',
					 '".$rs['e_receivertype']."',
					 '".$rs['e_sendertype']."',
					 '".$rs['e_type']."',
					 '".$_SESSION['useremail']."',
					 '".$_SESSION['realname']."',
					 '".$rs['e_fromemail']."',
					 '".$rs['e_fromname']."',
					 '".$rs['e_joid']."',
					 '".$_POST['subject']."',
					 '".$_POST['content']."',
					 '".$rs['e_contactid']."',
					'".time()."'
				)";
				$DB->query($sql);
			}

			//Send Email
			require_once("./include/class.phpmailer.php");
			
			foreach($to_emails as $key=>$single_email){
				$mail=new PHPMailer();

				$mail->IsMail();
				$mail->From     = $_SESSION['useremail'];
				$mail->FromName = $_SESSION['realname'];
				//foreach($to_emails as $key => $single_email){
				$mail->AddAddress($single_email,$to_names[$key]);
				//}

				$mail->IsHTML(true);
				$mail->Subject = stripslashes($_POST['subject']);
				$mail->Body = stripslashes($_POST['content']);

				$mail->CharSet = 'UTF-8';

				$mail->Send();
			}
			@unlink($file);

			
			$err  = "<h3 style=\"color:red\">Sent Successfully!</h3><meta http-equiv=\"refresh\" content=\"2;URL=myemails.php?op_folder=1\">";
	  }
}

//FCKeditor
$oFCKeditor = new FCKeditor("content") ;
$oFCKeditor->BasePath	= "FCKeditor/" ;
$oFCKeditor->Value = '' ;
$oFCKeditor->Width = 500;
$oFCKeditor->Height =300;
$oFCKeditor->ToolbarSet = "Basic";
$oFCKeditor->Value = "<br><br>-----------------------------<br><b>On ".date("m/d/Y",$rs['e_created']).", ".$rs['e_fromname']." wrote:</b><br>".$rs['e_content'];

$smarty->assign("fckcontent",$oFCKeditor->CreateHtml());


if($err!=''){
$smarty->assign("err",$err);
}
$smarty->assign("mycontacts",$contactOptions);
$smarty->assign("myjobs",$jobOptions);
$smarty->assign("search",$reg_data);
$smarty->display('allleft.tpl');
$smarty->display('replyemail.tpl');
$smarty->display('footer.tpl');

?>